package agent

import (
	"encoding/json"
	"fmt"
	"time"
	"week13/errors"
	Repo "week13/internal/data/agent"
	"week13/internal/tool"
)

const (
	STATE_PASS = 1 //渠道状态，审核通过
	STATE_REFUSE = 2 //渠道状态，拒绝
	LEVEL_V1 = 1 //渠道等级 V1
	LEVEL_V2 = 2 //渠道等级 V2
	LEVEL_V3 = 3 //渠道等级 V2
	)

var repository Repo.AgentReposity

func init() {
	repository = Repo.AgentReposityImp{}
}

func ChangeState(agentId int64, state int){
	agent,err := repository.Find(agentId)
	if  err != nil {
		return
	}
	nextState,err := Repo.NewState(state)
	if  err != nil {
		return
	}
	agent.StateTo(nextState)
}
func agentStatus(agentId int64){
	agent,err := repository.Find(agentId)
	if  err != nil {
		return
	}
	if agent.State.IsActive() == false{
		return
	}
}
var DefaultRepository Repo.AgentRepository = &Repo.AgentDao{}
type AgentDTO struct{
	AgentId int64 `json:"agentId"`
	Name string `json:"name"`
	Mobile string `json:"mobile"`
	Level int `json:"level"`
	State int `json:"state"`
	Balance int64 `json:"balance"`
	Addtime int64 `json:"addtime"`
	Mtime string `json:"mtime"`
	secret string `json:"secret"`
}

type Agent struct {
	AgentId int64
	DTO *AgentDTO
}
//创建渠道
func CreateAgent(dto AgentDTO,password string) (*Agent,error){
	if dto.Name == "" || dto.Mobile == ""  || password == "" {
		return nil, errors.NewIllegalParam("need name and mobile")
	}
	agentDo := DTOToDo(dto)
	agentDo.Password = password
	agentDo.Secret = tool.SHA1(fmt.Sprintf("%s%s%d",dto.Mobile,password,time.Now().Unix()))
	ret,err := DefaultRepository.Save(agentDo)
	if err != nil {
		return nil, errors.NewExecuteFail(err.Error())
	}
	return NewAgent(ret,nil)
}
//创建渠道DTO
func NewAgentDTO(jsonData []byte) (AgentDTO,error){
	dto := AgentDTO{}
	err := json.Unmarshal(jsonData,&dto)
	if err != nil {
		return AgentDTO{},err
	}
	return dto,nil
}
//实例化渠道
func NewAgent (agentId int64,DTO *AgentDTO) (*Agent,error){
	agent := &Agent{AgentId: agentId}
	if DTO != nil {
		agent.DTO = DTO
		return agent,nil
	}
	err := agent.fillAgent()
	if err != nil {
		return nil, err
	}
	return agent,err
}
//增加账户余额
func (agent *Agent) IncrBalance(amount int)(int64, error) {
	if amount<=0 {
		return 0,errors.NewIllegalParam(fmt.Sprintf("amount:%d error",amount))
	}
	return DefaultRepository.IncrBalance(agent.AgentId,amount)
}
//扣减账号余额
func (agent *Agent) DecrBalance(amount int)(int64, error) {
	if amount<=0 {
		return 0,errors.NewIllegalParam(fmt.Sprintf("amount:%d error",amount))
	}
	return DefaultRepository.DecrBalance(agent.AgentId,amount)
}
//设置渠道状态
func (agent *Agent) SetState(state int)(int64,error){
	if state != STATE_REFUSE && state != STATE_PASS{
		return 0,errors.NewIllegalParam("agent state illegal")
	}
	agentDo := Repo.AgentDO{State: state}
	return agent.save(agentDo)
}
//设置渠道等级
func (agent *Agent) SetLevel(level int)(int64,error){
	if level != LEVEL_V1 && level != LEVEL_V2 && level != LEVEL_V3{
		return 0,errors.NewIllegalParam("agent level illegal")
	}
	agentDo := Repo.AgentDO{Level: level}
	return agent.save(agentDo)
}
//保存渠道新
func (agent *Agent) Save(dto AgentDTO) (int64,error)  {
	agentDo := DTOToDo(dto)
	agentDo.AgentId = agent.AgentId
	ret,err := DefaultRepository.Save(agentDo)
	return ret,err
}
func (agent *Agent) save(do Repo.AgentDO)(int64,error){
	do.AgentId = agent.AgentId
	ret,err := DefaultRepository.Save(do)
	return ret,err
}

func (agent *Agent) fillAgent() error{
	Do,err := DefaultRepository.Find(agent.AgentId)
	if err != nil {
		return errors.NewExecuteFail(err.Error())
	}

	dto :=DoToDTO(Do)
	agent.DTO = &dto
	return nil
}
//查找渠道Do
func FindAgentDo(agentId int64) (Repo.AgentDO,error){
	Do,err := DefaultRepository.Find(agentId)
	if err != nil {
		return Repo.AgentDO{}, errors.NewExecuteFail(err.Error())
	}
	return Do,nil

}
//DTO 转换为 DO
func DTOToDo(dto AgentDTO) Repo.AgentDO{
	jsonData,_ := json.Marshal(dto)
	do := Repo.AgentDO{}
	json.Unmarshal(jsonData,&do)
	return do
}
// DO 转换为 DTO
func DoToDTO(do Repo.AgentDO) AgentDTO {
	jsonData,_ := json.Marshal(do)
	dto := AgentDTO{}
	json.Unmarshal(jsonData,&dto)
	return dto
}